//
//  LZFSeeBigPicController.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/3.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFSeeBigPicController.h"
#import "UIImageView+Download.h"
#import "LZFTopicItem.h"
#import <SVProgressHUD.h>

@interface LZFSeeBigPicController ()<UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView *scrollView;
@property(nonatomic,weak)UIImageView *imageView;
@property(nonatomic,strong)UIImage  *image;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;


@end
@implementation LZFSeeBigPicController



-(void)viewDidLoad
{
    //设置scrollview
    [self setUpScrollview];
    
    //设置imageview
    [self setUpImageView];
    
    //添加手势
    [self addTapGes];
    
}

 //设置scrollview
-(void)setUpScrollview
{
    UIScrollView *scrollView=[[UIScrollView alloc]init];
    scrollView.frame=[UIScreen mainScreen].bounds;
    [self.view addSubview:scrollView];
    scrollView.delegate=self;
    self.scrollView=scrollView;
    [self.view insertSubview:scrollView atIndex:0];
  
    
}

//设置imageview
-(void)setUpImageView
{
    UIImageView *imageView=[[UIImageView alloc]init];
    self.imageView=imageView;
    [self.scrollView addSubview:imageView];
    [imageView lzf_setImageWithOriginalImageURL:self.topicItem.large_image thumbnailImageURL:self.topicItem.small_image placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image==nil) nil;
        self.saveBtn.enabled=YES;
        self.image=image;
        
    }];
    
    imageView.xmg_x=0;
    imageView.xmg_width=screenW;
    imageView.xmg_height=screenW*self.topicItem.height/self.topicItem.width;
    
    if (self.topicItem.isBigPicture) {
        //大图
        imageView.xmg_y=0;
        self.scrollView.contentSize=CGSizeMake(0, imageView.xmg_height);
    }
    else
    {
        //非大图
        imageView.xmg_centerY=self.view.xmg_centerY;
    }
    
    
    //算scrollview的缩放比例
    CGFloat maxScale= self.topicItem.height/imageView.xmg_height;
    self.scrollView.maximumZoomScale=maxScale;
}

  //添加手势
-(void)addTapGes
{
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
}


-(void)tap
{
    [self clickBack:nil];
}

- (IBAction)clickBack:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击保存按钮,保存图片到系统相册中
- (IBAction)clickSave:(id)sender {
    
    UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }
    else
    {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        
    }
}

//scrollviewdelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

@end
